package javacompiler.syntax;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SymbolsTableTest {

	private SymbolsTable table;
	
	private static final long id1 = 1;
	private static final long id2 = 2;
	private static final long id3 = 3;
	private static final long id4 = 4;
	
	private JavaSymbolInfo symbol1;
	private JavaSymbolInfo symbol2;
	private JavaSymbolInfo symbol3;
	private JavaSymbolInfo symbol4;
	
	@Before
	public void setUp() {
		table = new SymbolsTable();
	}
	
	@Test
	public void testSameParameters1() {
		createSymbolsSameParameters();
		
		table.addSymbol(id1, symbol1);
		table.addSymbol(id2, symbol2);
		table.addSymbol(id3, symbol3);
		table.addSymbol(id4, symbol4);
		
		Assert.assertTrue(table.sameParameters(id3, id4));
	}
	
	@Test
	public void testSameParameters2() {
		createSymbolsDifferentParameters1();
		
		table.addSymbol(id1, symbol1);
		table.addSymbol(id2, symbol2);
		table.addSymbol(id3, symbol3);
		table.addSymbol(id4, symbol4);
		
		Assert.assertFalse(table.sameParameters(id3, id4));
	}

	@Test
	public void testSameParameters3() {
		createSymbolsDifferentParameters2();
		
		table.addSymbol(id1, symbol1);
		table.addSymbol(id2, symbol2);
		table.addSymbol(id3, symbol3);
		table.addSymbol(id4, symbol4);
		
		Assert.assertFalse(table.sameParameters(id3, id4));
	}
	
	private void createSymbolsSameParameters() {
		symbol1 = new JavaSymbolInfo();
		symbol1.setVariableType("Type1");
		
		symbol2 = new JavaSymbolInfo();
		symbol2.setVariableType("Type1");
		
		symbol3 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol3 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol3.add(symbol1);
		symbol3.setParametersInfo(parametersSymbol3);
		
		symbol4 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol4 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol4.add(symbol2);
		symbol4.setParametersInfo(parametersSymbol4);
	}
	
	private void createSymbolsDifferentParameters1() {
		symbol1 = new JavaSymbolInfo();
		symbol1.setVariableType("Type1");
		
		symbol2 = new JavaSymbolInfo();
		symbol2.setVariableType("Type2");
		
		symbol3 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol3 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol3.add(symbol1);
		symbol3.setParametersInfo(parametersSymbol3);
		
		symbol4 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol4 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol4.add(symbol2);
		symbol4.setParametersInfo(parametersSymbol4);
	}
	
	private void createSymbolsDifferentParameters2() {
		symbol1 = new JavaSymbolInfo();
		symbol1.setVariableType("Type1");
		
		symbol2 = new JavaSymbolInfo();
		symbol2.setVariableType("Type2");
		
		symbol3 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol3 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol3.add(symbol1);
		parametersSymbol3.add(symbol2);
		symbol3.setParametersInfo(parametersSymbol3);
		
		symbol4 = new JavaSymbolInfo();
		List<JavaSymbolInfo> parametersSymbol4 = new ArrayList<JavaSymbolInfo>();
		parametersSymbol4.add(symbol2);
		parametersSymbol4.add(symbol1);
		symbol4.setParametersInfo(parametersSymbol4);
	}
}
